package pl.edu.agh.iosr.gamblingzone.adminpanel.action;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.Destroy;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.log.Log;

import pl.edu.agh.iosr.gamblingzone.dao.DisciplineDAO;
import pl.edu.agh.iosr.gamblingzone.model.Discipline;
import pl.edu.agh.iosr.gamblingzone.utils.DAOFactory;

@Stateful
@Name("disciplinesManage")
@Scope(ScopeType.SESSION)
public class DisciplinesManageAction implements DisciplinesManage {

	@Logger
	Log log;

	@PersistenceContext
	EntityManager entityManager;

	/** The dao. */
	@In(required=true)
	DAOFactory dao;

	private List<Discipline> all;
	private DisciplineDAO disciplineDAO;
	private String newDisciplineName;
	private String newDisciplineDesc;

	@Create
	public void init() {
		disciplineDAO = dao.getDisciplineDAO();
		newDisciplineName = "";
		newDisciplineDesc = "";
		all = new ArrayList<Discipline>();
	}

	@Remove
	public void remove() {
		// DO NOTHING
	}

	public void addDiscipline() {
		if ((!(newDisciplineName.equals(""))) && (!(newDisciplineDesc.equals("")))) {
			Discipline discipline = new Discipline();
			discipline.setName(newDisciplineName);
			discipline.setDescription(newDisciplineDesc);
			disciplineDAO.makePersistent(discipline);
		}
		newDisciplineDesc = "";
		newDisciplineName = "";
	}

	public void removeDiscipline(Discipline d) {
		disciplineDAO.makeTransient(d);
	}

	public String getNewDisciplineName() {
		return newDisciplineName;
	}

	public String getNewDisciplineDesc() {
		return newDisciplineDesc;
	}

	public void setNewDisciplineName(String newDisciplineName) {
		this.newDisciplineName = newDisciplineName;
	}

	public void setNewDisciplineDesc(String newDisciplineDesc) {
		this.newDisciplineDesc = newDisciplineDesc;
	}

	public List<Discipline> getAll() {
		all = disciplineDAO.findAll();
		return all;
	}
}
